<template>
    <div class="content-wrapp">
        <el-form ref="formData" :model="formData" label-width="80px">
            <el-form-item label="部门名称" prop="deptName">
                <el-input v-model="formData.deptName" placeholder="请输入" size="small"></el-input>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select v-model="formData.status" placeholder="请选择活动区域" size="small">
                    <el-option label="全部" value></el-option>
                    <el-option label="正常" value="0"></el-option>
                    <el-option label="禁用" value="1"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label-width="20px">
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    size="small"
                    @click="loadDeptList"
                >搜索</el-button>
                <el-button
                    icon="el-icon-circle-close"
                    size="small"
                    @click="$refs['formData'].resetFields()"
                >重置</el-button>
                <el-button
                    type="success"
                    icon="el-icon-circle-plus-outline"
                    size="small"
                    @click="$refs.modal.add()"
                    v-if="addEnable"
                >新增</el-button>
            </el-form-item>
        </el-form>
        <el-table
            :data="tableData"
            row-key="deptId"
            default-expand-all
            :tree-props="{children: 'children'}"
            v-loading="loading" 
        >
            <el-table-column prop="deptName" label="权限名称"></el-table-column>
            <el-table-column prop="orderNum" label="排序"></el-table-column>
            <el-table-column prop="status" label="状态">
                <template slot-scope="scope">{{scope.row.status | statusFilter}}</template>
            </el-table-column>
            <el-table-column prop="createTime" label="创建时间"></el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button
                        type="success"
                        size="mini"
                        @click="$refs.modal.edit(scope.row)"
                        v-if="editEnabel"
                    >编辑</el-button>
                    <el-button
                        v-if="addEnable"
                        type="primary"
                        size="mini"
                        @click="$refs.modal.add(scope.row.deptId)"
                    >新增</el-button>
                    <el-button
                        v-if="removeEnable"
                        type="danger"
                        size="mini"
                        @click="removeDept(scope.row.deptId)"
                    >删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <dept-modal ref="modal" @ok="loadDeptList" />
    </div>
</template>

<script>
import { getDeptList, delDept } from "@/api/system";
import { treeData } from "@/utils/treeutil";
import DeptModal from "./modules/DeptModal.vue";
import { checkPermission } from "@/utils/permissions";
const ERR_OK = 200;
export default {
    components: {
        DeptModal
    },
    data() {
        return {
            formData: {
                deptName: "",
                status: ""
            },
            tableData: [],
            loading: true,
            addEnable: checkPermission("system:dept:add"),
            editEnabel: checkPermission("system:dept:edit"),
            removeEnable: checkPermission("system:dept:remove")
        };
    },
    filters: {
        statusFilter(status) {
            const statusMap = {
                "1": "停用",
                "0": "正常"
            };
            return statusMap[status];
        }
    },
    created() {
        this.loadDeptList();
    },
    methods: {
        loadDeptList() {
            this.loading = true;
            getDeptList(this.formData).then(res => {
                this.tableData = treeData(res.data.rows, "deptId");
                this.loading = false;
            });
        },
        removeDept(id) {
            this.$confirm("此操作将永久删除该部门, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            })
                .then(() => {
                    delDept(id).then(res => {
                        if (res.data.code === ERR_OK) {
                            this.$message.success(`删除成功`);
                            this.loadDeptList();
                        } else {
                            this.$message.error(res.data.msg);
                        }
                    });
                })
                .catch(() => {});
        }
    }
};
</script>

<style lang="scss" scoped>
.content-wrapp {
    padding: 20px;
    .el-form-item {
        display: inline-block;
        margin-bottom: 10px;
    }
}
</style>
